﻿package com.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
//数据库工具类
public class MySQLIteOpenHelper extends SQLiteOpenHelper{
	public final static int VERSION = 1;// 版本号
	 public final static String TABLE_NAME = "FileName";// 存储创建PDF图片文件表
	 public final static String File_Name = "File_Name";// 存储我的文档文件表
	 
	 public static final String DATABASE_NAME = "Document.db";//数据库名称
	 public  static SQLiteDatabase db;
	 public MySQLIteOpenHelper(Context context) {	 
	  super(context, DATABASE_NAME, null, VERSION); 
	 } 
	 @Override
	 		// 在数据库第一次生成的时候会调用这个方法，一般我们在这个方法里边生成数据库表;
	 public void onCreate(SQLiteDatabase db) { 
		
		
		 this.db=db;
		 String str_sql="create table FileName(File_Name text)";
		 String str_sql_1="create table ListFile(ListFile_Name text)";
			 db.execSQL(str_sql);
			 db.execSQL(str_sql_1);
			
			 }
	
	 
	 public void insert(String sql) { 
		SQLiteDatabase db=getWritableDatabase();
		
	db.execSQL(sql);
	
		 }
	 public void update_1(String sql){
		 SQLiteDatabase db=getWritableDatabase();
		 db.execSQL(sql);
		
		
	 }

	 @Override
	 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	 } 
	 @SuppressWarnings("null")
	 //查询方法 得到List 显示在ListView上
	public List select(String sql,String []selectvalues) {
		 
		 db=getWritableDatabase();
		 //查询获得cursor
		 ArrayList returnList=new ArrayList();
		 Cursor c=db.rawQuery(sql, selectvalues); 
		 while(c.moveToNext()){
			 HashMap rowMap=new HashMap();
			 int selectColCount=c.getColumnCount();
			 for(int i=0;i<selectColCount;i++)
			 {
				 String [] colName=c.getColumnNames();
				 for(int j=0;j<colName.length;j++)
				 {
					 int colIdex=c.getColumnIndex(colName[j]);
					 String colvalue=c.getString(colIdex);
					 rowMap.put(colName[j], colvalue);
				 }
			 }
			 returnList.add(rowMap);
			 
		
		 }
		 c.close();
		
		return returnList;
		
		
	      	  }
	 //删除数据
	 public void delete(String sql)
	 {
		 SQLiteDatabase db=getWritableDatabase();
		 db.execSQL(sql);
		
		
	 }
	 public ArrayList  select(String sql){
		 db=getWritableDatabase();
		 //查询获得cursor
		 ArrayList<String> returnList=new ArrayList();
		 Cursor c=db.rawQuery(sql, null); 
		 c.moveToFirst();
		
			 int Count=c.getCount();
			 for(int i=0;i<Count;i++){
				 returnList.add(c.getString(0));
				 c.moveToNext();
			 }
			 c.close();
			
		 
		return returnList;
	 }

	}
